import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { DomSanitizer } from "@angular/platform-browser";
import {HttpURLService} from "../httpURL.service";
import { LocalStorage } from "../local.storage";

@Component({
  moduleId: module.id,
  selector: 'myLogin',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class loginComponent implements OnInit {
  public url:string = this.httpURLService.getUrl("communication");
  constructor(public router:Router,private http: HttpClient,private sanitizer: DomSanitizer,private httpURLService: HttpURLService) {

  }
  loginName:string;
  name:string;
  password:string;
  isLogin : boolean = true;
  loginMessage : string;
  public LoginQuery:Login = new Login("","");
  checked:boolean = false;
  doLogin(info:any) {
    // console.log(info);
    this.name = info.loginName;
    this.password = info.password;
    this.isLogin = false;
    this.LoginQuery = new Login(info.userName,info.password);
    //判断是否保存用户名
    if (this.checked == true) {
      //存储到loaclStage
      localStorage["loginName"] = info.loginName;
      localStorage["password"] = info.password;
      localStorage["state"] = "yes";
    } else {
      localStorage["loginName"] = "";
      localStorage["password"] = "";
      localStorage["state"] = "no";
    }
    this.http.post(this.url + '/user/login', info).subscribe(data => {
      if (data['result'].resultCode == '101111001') {
        this.http.get('api/data.json').subscribe(data => {
          if ((data['result'].resultCode == '0') && (info.loginName == data['result'].userName) && (( info.password == data['result'].password))) {
            localStorage["userID"] = data["userId"];
            this.router.navigate(['/index/home']);
          }
        });
      } else if (data['result'].resultCode == '0') {
        // localStorage["userID"] = data["userId"];
        sessionStorage["userID"] = data["userId"];
        this.router.navigate(['/index/home']);
      } else if (data['result'].resultCode == '101111001') {
        this.loginMessage = "用户名或密码错误";
        this.isLogin = false;
      } else if (data['result'].resultCode == '101111002') {
        this.loginMessage = "验证码错误";
        this.isLogin = false;
        // this.http.get('api/data.json').subscribe(data => {
        //   if((data['result'].resultCode=='0')&&(info.userName==data['result'].userName)&&(( info.password==data['result'].password))){
        //     this.router.navigate(['/index/home']);
        //   }else{
        //     this.loginMessage="用户名或密码错误";
        //     this.isLogin = false;
        //   }
        // });
      }
    })
  }

  ngOnInit() {
    if("yes" == localStorage["state"]){
      //$("#remb_me").attr("checked", true);
      this.checked=true;
      this.name=localStorage["loginName"];
      this.password=localStorage["password"];
    }
  }
}
export class Login{
  constructor(
    private  account:string,//用户id
    private  password:string, //用户密码
  ){}
}
